package cn.stylefeng.guns.modular.rest.transport;

import cn.hutool.json.JSONUtil;
import cn.stylefeng.guns.modular.rest.aop.UserAop;
import cn.stylefeng.guns.modular.transport.entity.TransportReceipt;
import cn.stylefeng.guns.modular.transport.model.params.TDispatchDriverEvalParam;
import cn.stylefeng.guns.modular.transport.model.result.StoreJsonOV;
import cn.stylefeng.guns.modular.transport.model.result.TDispatchDriverEvalResult;
import cn.stylefeng.guns.modular.transport.model.result.TDispatchResult;
import cn.stylefeng.guns.modular.transport.service.TDispatchDriverEvalService;
import cn.stylefeng.guns.modular.transport.service.TDispatchService;
import cn.stylefeng.guns.modular.transport.service.TransportReceiptService;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.net.URLDecoder;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/rest/tDispatchDriverEval")
public class RestTDispatchDriverEvalController {

    @Autowired
    private UserAop userAop;

    @Autowired
    private TDispatchDriverEvalService tDispatchDriverEvalService;

    @Autowired
    private TDispatchService tDispatchService;

    @Autowired
    private TransportReceiptService transportReceiptService;

    @RequestMapping("/getInfoByDispatchNumber")
    @ResponseBody
    public ResponseData getInfoByDispatchNumber(TDispatchDriverEvalParam tDispatchDriverEval) {
        //根据运输单号，查询门店
        List<TransportReceipt> list = transportReceiptService.lambdaQuery().eq(TransportReceipt::getDispatchNumber,tDispatchDriverEval.getDispatchNumber()).list();
        TDispatchDriverEvalResult tDispatchDriverEvalResult = this.tDispatchDriverEvalService.findBySpec(tDispatchDriverEval);
        if(ToolUtil.isEmpty(tDispatchDriverEvalResult)){
            tDispatchDriverEvalResult = new TDispatchDriverEvalResult();
            tDispatchDriverEvalResult.setPayFlag("否");
            tDispatchDriverEvalResult.setWhLoadScore(3);
            tDispatchDriverEvalResult.setWhServScore(3);
            for(TransportReceipt obj : list){
                tDispatchDriverEvalResult.getStoreList().add(StoreJsonOV.builder()
                        .storeName(obj.getGetUnit())
                        .unloadingSpeed(3)
                        .storeAttitude(3)
                        .build());
            }
        }else{
            if(ToolUtil.isNotEmpty(tDispatchDriverEvalResult.getStoreJson())){
                List<StoreJsonOV> storeJsonOVS = JSONArray.parseArray(tDispatchDriverEvalResult.getStoreJson(), StoreJsonOV.class);
                tDispatchDriverEvalResult.setStoreList(storeJsonOVS);
            }

        }
        return ResponseData.success(tDispatchDriverEvalResult);
    }


    @RequestMapping("/add")
    @ResponseBody

    public ResponseData addItem(@RequestBody TDispatchDriverEvalParam tDispatchDriverEvalParam) {
        User user = userAop.getUser();
//        String decode = URLDecoder.decode(formData);
//        TDispatchDriverEvalParam tDispatchDriverEvalParam = JSONObject.parseObject(decode, TDispatchDriverEvalParam.class);
        tDispatchDriverEvalParam.setStoreJson(JSONUtil.toJsonStr(tDispatchDriverEvalParam.getStoreList()));
        TDispatchResult tDispatchObj = tDispatchService.findByDispatchNumber(tDispatchDriverEvalParam.getDispatchNumber());
        tDispatchDriverEvalParam.setDeptId(tDispatchObj.getDeptId());
        tDispatchDriverEvalParam.setOperateId(tDispatchObj.getOperateId());
        tDispatchDriverEvalParam.setOperateName(tDispatchObj.getOperateName());
        tDispatchDriverEvalParam.setCreateUser(user.getUserId());
        tDispatchDriverEvalParam.setCreateTime(new Date());
        tDispatchDriverEvalParam.setDeliverId(1L);
        this.tDispatchDriverEvalService.add(tDispatchDriverEvalParam);
        return ResponseData.success();
    }


}
